<?php
require_once("../config/dbconn.php");
set_time_limit(0);
$fenxiangcaiwu = $db->get_all("SELECT count(userid) as num,userid from wd_caiwu where addtime>1528387200 and ptype=4 and type=5 GROUP BY userid ORDER BY num DESC limit 0,300");
foreach ($fenxiangcaiwu as $key=>$value){
    $userid=$value['userid'];
    echo $key.",".$userid."<br/>";
    //获取某个会员8号全部的1级算力分享记录
    $getoneusercaiwu = $db->get_all("select id,price,memo,addtime,isff from wd_caiwu where userid={$userid} and addtime>1528387200 and ptype=4 and type=5 order by memo asc");
    //统计出处理前会员算力分享奖金额
    $newusercaiwu = $db->get_one("select sum(price) as c from wd_caiwu where userid={$userid} and addtime>1528387200 and ptype=4 and type=5 order by memo asc");

    //处理前金额
    $original = $newusercaiwu['c'];
    $leijimoney = 0;
    foreach ($getoneusercaiwu as $k=>$v){
        if($v['isff'] == 1){
            continue;
        }
        //针对每一条记录做处理
        $fenxiang=explode("：",$v['memo']);

        //统计该情况数量
$chasql="select count(id) as c,id from wd_caiwu where userid={$userid} and memo like '%{$fenxiang[2]}%' and addtime>1528387200";
        $statistics = $db->get_one($chasql);
        if($statistics['c'] >=2){
            //更新该会员某一个分享算力来源
            $sql = "update wd_caiwu set isff=1 where userid={$userid} and memo like '%{$fenxiang[2]}%' and addtime>1528387200 and id !={$statistics['id']}";
            $db->query($sql);
        }
    }
    //处理后算力分享奖金额
    $newusercaiwu1 = $db->get_one("select sum(price) as c from wd_caiwu where userid={$userid} and addtime>1528387200 and ptype=4 and type=5 and isff=0 order by memo asc");
    //真实的算力分享奖
    $real_money = $newusercaiwu1['c'];
    if($original == $real_money){
        file_put_contents('./Qualified.txt', "跳过会员:".$value['id']."，处理前算力分享奖金金：".$original."，处理后算力分享奖金：".$real_money."-------".PHP_EOL.PHP_EOL,FILE_APPEND);
        continue;
    }
    file_put_contents('./unqualified.txt', "处理会员:".$value['id']."，处理前算力分享奖金金：".$original."，处理后算力分享奖金：".$real_money."-------".PHP_EOL.PHP_EOL,FILE_APPEND);
    //需扣除金额
    $deduct = $original - $real_money;
    //查询该会员账户余额
    //查询该会员算力钱包余额是否充足
    $user=$db->get_one("select id,username,slprice from wd_users where id={$userid}");
    //余额不足
    if($user['slprice'] <$deduct){
        file_put_contents('./abnormal_member.txt', "余额不足会员:".$user['username']."，算力钱包剩余:".$user['slprice']."需扣除金额：".$deduct."-------".PHP_EOL,FILE_APPEND);
        continue;
    }
    //余额充足
    $db->query('start transaction');#开启事物
    try {
        //更新该会员算力钱包余额
        $row = $db->query("update wd_users set slprice=slprice-{$deduct},ljslprice=ljslprice={$deduct} where id={$value['userid']}");
        //异常
        if(empty($row)){
            throw new Exception('更新失败');
        }
        file_put_contents('./update.sql', "update wd_users set slprice=slprice-{$up_money},ljslprice=ljslprice={$up_money} where id={$value['userid']};".PHP_EOL,FILE_APPEND);
        #会员资金更新成功，删除财务记录
        $caiwusql="delete from wd_caiwu where userid={$userid} and addtime>1528387200 and ptype=4 and type=5 and isff=1";
        $caiwu_lst=$db->query($caiwusql);
        if(empty($caiwu_lst)){
            throw new Exception('财务记录删除失败');
        }
        //记录删除的订单sql
        file_put_contents('./delete_caiwu.sql', "delete from wd_caiwu where userid={$userid} and addtime>1528387200 and ptype=4 and type=5 and isff=1".PHP_EOL,FILE_APPEND);
        $db->query('commit');#提交
    }catch(Exception $e){
        $db->query('rollback');#回滚
        returnJson(0,$e->getMessage());
    }
}
